package com.vcredit.bird.vo.ympush;

import java.io.Serializable;
import java.util.Map;
import java.util.Map.Entry;

import com.fasterxml.jackson.annotation.JsonProperty;

public class Notification implements Serializable{

	private static final long serialVersionUID = -9222854748805561644L;

	private String appKey; // 必填 应用唯一标识
	
	private String timestamp; // 必填 时间戳，10位或者13位均可，时间戳有效期为10分钟
	
	// 必填 消息发送类型 unicast、listcast、filecast、broadcast、groupcast、customizedcast
	private String type; 
	
	@JsonProperty("device_tokens")
	private String deviceTokens; // 可选 设备唯一表示
	
	@JsonProperty("alias_type")
	private String aliasType; // 可选 当type=customizedcast时，必填
	
	private String alias; // 可选 当type=customizedcast时, 开发者填写自己的alias
	
	@JsonProperty("file_id")
	private String fileId; // 可选 当type=filecast时，file内容为多条device_token
	
	private Map<String, Object> filter; // 可选  终端用户筛选条件,如用户标签、地域、应用版本以及渠道等
	
	private Payload payload; // 必填 消息内容(Android最大为1840B)
	
	private Map<String,String> extra;  // 可选 用户自定义key-value
	
	private	Policy policy; // 可选 发送策略
	
	@JsonProperty("production_mode")
	private String productionMode; // 可选 正式/测试模式。
	
	private String description; // 可选 发送消息描述，建议填写。
	
	public Notification() {
		super();
	}
	
	public Notification(Builder builder) {
		this.appKey = builder.appKey;
		this.timestamp = builder.timestamp;
		this.type = builder.type;
		this.deviceTokens = builder.deviceTokens;
		this.aliasType = builder.aliasType;
		this.alias = builder.alias;
		this.fileId = builder.fileId;
		this.filter = builder.filter;
		this.payload = builder.payload;
		this.extra = builder.extra;
		this.policy = builder.policy;
		this.productionMode = builder.productionMode;
		this.description = builder.description;
	}
	
	public String getAppKey() {
		return appKey;
	}

	public void setAppKey(String appKey) {
		this.appKey = appKey;
	}

	public String getTimestamp() {
		return timestamp;
	}

	public void setTimestamp(String timestamp) {
		this.timestamp = timestamp;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getDeviceTokens() {
		return deviceTokens;
	}

	public void setDeviceTokens(String deviceTokens) {
		this.deviceTokens = deviceTokens;
	}

	public String getAliasType() {
		return aliasType;
	}

	public void setAliasType(String aliasType) {
		this.aliasType = aliasType;
	}

	public String getAlias() {
		return alias;
	}

	public void setAlias(String alias) {
		this.alias = alias;
	}

	public String getFileId() {
		return fileId;
	}

	public void setFileid(String fileId) {
		this.fileId = fileId;
	}

	public Payload getPayload() {
		return payload;
	}

	public void setPayload(Payload payload) {
		this.payload = payload;
	}

	public Map<String, String> getExtra() {
		return extra;
	}

	public void setExtra(Map<String, String> extra) {
		this.extra = extra;
	}

	public Policy getPolicy() {
		return policy;
	}

	public void setPolicy(Policy policy) {
		this.policy = policy;
	}

	public String getProductionMode() {
		return productionMode;
	}

	public void setProductionMode(String productionMode) {
		this.productionMode = productionMode;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}
	
	public Map<String, Object> getFilter() {
		return filter;
	}

	public void setFilter(Map<String, Object> filter) {
		this.filter = filter;
	}


	public static class Builder {
		
		private String appKey; // 必填 应用唯一标识
		
		private String timestamp; // 必填 时间戳，10位或者13位均可，时间戳有效期为10分钟
		
		// 必填 消息发送类型 unicast、listcast、filecast、broadcast、groupcast、customizedcast
		private String type; 
		
		private String deviceTokens; // 可选 设备唯一表示
		
		private String aliasType; // 可选 当type=customizedcast时，必填
		
		private String alias; // 可选 当type=customizedcast时, 开发者填写自己的alias
		
		private String fileId; // 可选 当type=filecast时，file内容为多条device_token
		
		private Map<String,Object> filter; // 可选 终端用户筛选条件,如用户标签、地域、应用版本以及渠道等
		
		private Payload payload; // 必填 消息内容(Android最大为1840B)
		
		private Map<String,String> extra;  // 可选 用户自定义key-value
		
		private	Policy policy; // 可选 发送策略
		
		private String productionMode; // 可选 正式/测试模式。
		
		private String description; // 可选 发送消息描述，建议填写。
		
        public Builder() {
			super();
		}
        
		public Builder(String appKey, String type) {  
            this.appKey = appKey;  
            this.type = type;  
        }
		
		public Notification build() {
			return new Notification(this);
		}
		
		public Builder appKey(String appKey) {  
            this.appKey = appKey;  
            return this;  
        } 
		
		public Builder timestamp(String timestamp) {
			this.timestamp = timestamp;
			return this;
		}
		
		public Builder type(String type) {
			this.type = type;
			return this;
		}
		
		public Builder deviceTokens(String deviceTokens) {
			this.deviceTokens = deviceTokens;
			return this;
		}
		
		public Builder aliasType(String aliasType) {
			this.aliasType = aliasType;
			return this;
		}
		
		public Builder alias(String alias) {
			this.alias = alias;
			return this;
		}
		
		public Builder fileId(String fileId) {
			this.fileId = fileId;
			return this;
		}
		
		public Builder filter(Map<String,Object> filter) {
			this.filter = filter;
			return this;
		}
		
		public Builder payload(Payload payload) {
			if(payload instanceof IosPayload) {
				IosPayload iosPayload = (IosPayload)payload;
				payload.put("aps", iosPayload.getAps());
				Map<String, String> iosExtra = iosPayload.getIosExtra();
				if(iosExtra != null && iosExtra.size() > 0) {
					for (Entry<String, String> entry : iosExtra.entrySet()) {
						payload.put(entry.getKey(), entry.getValue());
					}
				}
				
			} else if(payload instanceof AndroidPayload) {
				AndroidPayload androidpayload = (AndroidPayload)payload;
				payload.put("displayType", androidpayload.getDisplayType());
				payload.put("body", androidpayload.getBody());
			} else {
				throw new RuntimeException("com.vcredit.bird.vo.ympush.Payload class type is not validated!");
			}
			this.payload = payload;
			return this;
		}
		
		public Builder extra(Map<String,String> extra) {
			this.extra = extra;
			return this;
		}
		
		public Builder policy(Policy policy) {
			this.policy = policy;
			return this;
		}
		
		public Builder productionMode(String mode) {
			this.productionMode = mode;
			return this;
		}
		
		public Builder description(String description) {
			this.description = description;
			return this;
		}
	}
}
